<!--
 * @Author: 梁 卓
 * @Version: V1.0.0
 * @Date: 2024-02-24 10:09:04
 * @LastEditors: 梁 卓
 * @LastEditTime: 2024-02-27 15:20:16
 * @FilePath: borrowinfoadd.html
 * Copyright 2024 Marvin, All Rights Reserved. 
 * 2024-02-24 10:09:04
 * @Description: 
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新增借阅信息</title>
    <script src="../../scripts/boot.js" type="text/javascript"></script>
    <script src="../../demo/demo.css" type="text/javascript"></script>

    <style type="text/css">
    html, body
    {        
        padding:0;
        margin:0;
        border:0;
        height:100%;
        overflow:hidden;
    }
    .asLabel .mini-textbox-border,
    .asLabel .mini-textbox-input,
    .asLabel .mini-buttonedit-border,
    .asLabel .mini-buttonedit-input,
    .asLabel .mini-textboxlist-border
    {
        border:0;background:none;cursor:default;
    }
    .asLabel .mini-buttonedit-buttons,
    .asLabel .mini-buttonedit-button,
    .asLabel .mini-textboxlist-close
    {
        display:none;
    }
    .asLabel .mini-textboxlist-item
    {
        padding-right:8px;
    }    
    </style>
</head>
<body>
  <form id="borrowinfoform" method="post">
    <input name="id" class="mini-hidden" />
    <fieldset style="border:solid 1px #aaa;padding:3px;">
        <legend >请填写借阅信息</legend>
        <div style="padding:5px;">
    <table>
        <tr>
            <td align="right">剩余数量：</td>
            <td >    
                <input name="remain" id="idRmain" class="mini-textbox asLabel" value="0" readonly/>
            </td>
        </tr>

        <tr>
          <td align="right">所借图书：</td>
            <td >
                <input name="bookId" id="idBookInfo" class="mini-combobox" allowInput="true" 
                  textField="bookName" valueField="bookId" onValuechanged="onChangeBookInfo"
                  required="true" requiredErrorMessage="图书类别不能为空！"/>
            </td>

            <td align="right">借阅编号：</td>
            <td >    
                <input name="borrowid" id="idBorrowid" class="mini-textbox asLabel" readonly/>
            </td>

            
        </tr>

        <tr>
          <td align="right">借阅人：</td>
          <td >    
              <input name="borrower" class="mini-textbox" 
                required requiredErrorText="姓名不能为空！"/>
          </td>
          <td align="right">联系电话：</td>
            <td >    
                <input name="phone" class="mini-textbox"
                onValidation="onPhoneValidation"/>
            </td>
        </tr> 
        
        <tr>
          <td align="right">借阅时间：</td>
          <td >    
            <input name="borrowtime" id="idBorrowTime" class="mini-datepicker" nullValue="null"
              format="yyyy-MM-dd H:mm:ss" timeFormat="HH:mm:ss" showTime="true"
              showOkButton="true" showClearButton="false" ondrawdate="onDrawDate"/>
          </td>
        </tr> 
    </table>            
        </div>
    </fieldset>
    <div style="text-align:center;padding:10px;">               
        <a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>       
        <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
    </div>        
  </form>


  <script type="text/javascript">
   mini.parse();

   var form = new mini.Form("borrowinfoform");

   form.setChanged(false)

  /////////////通id设置值///////////////////////
  // 获取当前时间
  function getnowTime() {
    mini.get('idBorrowTime').setValue(new Date())
  }


  /////////////相关控件事件///////////////////////
  //当所借图书下拉框改变事件
  function onChangeBookInfo(e) {
    mini.get('idRmain').setValue(e.selected.remain)
  }


  // 手机号校验
  function onPhoneValidation(e) {
    const phoneRegex = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
    const phoneNumber = e.value;
    if (!phoneRegex.test(phoneNumber)) {
      e.errorText = "联系电话格式错误";
      e.isValid = false;
    } else {
      e.isValid = true;
    }
  }

  // 禁用不能选用的日期
  function onDrawDate(e) {
    var date = e.date;
    var d = new Date();

    if (date.getTime() > d.getTime()) {
        e.allowSelect = false;
    }
  }

  // 提交按钮事件
  function onOk() {
    var o = form.getData()
    form.validate();
    if (form.isValid() == false) {
        mini.alert("缺少填写某些内容", '错误');
        return;
    }
    if (o.remain  <= 0) {
      mini.alert('该图书库存不足，无法借阅', '错误');
      return ;
    }
    if (o.borrowtime.getTime() > new Date().getTime()) {
      mini.alert('借阅时间不能大于当前时间', '错误');
    }
    // 提交数据
    var json = mini.encode(o)
    addBorrowInfoAjax(json)
  }

  // 取消按钮事件
  function onCancel() {
    CloseWindow('close')
  }

  function setData(data) {
    // 先克隆
    data = mini.clone(data)

    if (data.action === 'bookinfolist') {
      var bookid = data.bookid
      // 获取借阅编号
      getBorrowId()
      // 获取当前时间
      getnowTime()
      // 获取所有图书信息
      getAllBookInfo(bookid)

    } else if (data.action === 'borrowinfolist') {
      // 获取借阅编号
      getBorrowId()
      // 获取当前时间
      getnowTime()
      // 获取所有图书信息
      getAllBookInfo(null)
    }

  }

  //////////////////ajax方法//////////////////////
   // 获取借阅编号
   function getBorrowId() {
      $.ajax({
        url: "/bookmanagement/borrowinfolistaction?method=generalBorrowId",
        type: 'GET',
        cache: false,
        success: function (res) {
          var id = mini.decode(res)
          mini.get('idBorrowid').setValue(id)
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.responseText);
        }
      });
   }

   // 获取所有图书信息
   function getAllBookInfo(bookid) {
      $.ajax({
        url: "/bookmanagement/borrowinfolistaction?method=queryBookinfoByKeyWords",
        type: 'GET',
        cache: false,
        success: function (res) {
          var input = mini.get('idBookInfo')
          var o = mini.decode(res)
          // 如果从图书信息页面进行借阅，则将所选图书默认为该借阅下的图书
          if (bookid == null || bookid == '' || bookid == undefined) {
            input.load(o)
          } else {
            o.forEach(item => {
              if (item.bookId == bookid) {
                input.load([item]);
                // 设置默认值
                input.setValue(bookid)
                // 只读
                input.setReadOnly(true) 
                // 增加asLabel外观
                input.addCls("asLabel") 
                // 设置剩余数量
                mini.get('idRmain').setValue(item.remain)
                return ;
              }
            })
          }
          
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.responseText);
        }
      });
   }

   // 新增借阅信息
   function addBorrowInfoAjax(json) {
      $.ajax({
          url: "/bookmanagement/borrowinfoaddaction?method=addBorrowInfo",
          type: 'post',
          data: { data: json },
          cache: false,
          success: function (res) {
              mini.alert(res, '提示',function() {
                  mini.alert(res, '提示')
                  CloseWindow("save");
              })
          },
          error: function (jqXHR, textStatus, errorThrown) {
              alert(jqXHR.responseText);
              CloseWindow();
          }
      });
   }

//////////////////辅助函数//////////////////////
   // 关闭窗口
   function CloseWindow(action) {
      if (action === "close" && form.isChanged()) {
          mini.confirm('填写的数据将被清空', '提示', function(callTxt) {
              // 确定 ok, 取消 cancel ，修改数据且点击了取消按钮则提示保存
              if (callTxt === 'ok') {
                  onOk()
              } else {
                window.CloseOwnerWindow(action)
              }
          })
      } else {
          window.CloseOwnerWindow(action)
      }
      
  }

  </script>
</body>
</html>